﻿package 
{
	import com.event.EventZheng;
	import com.greensock.*;
	import com.greensock.easing.*;
	import com.utils.DebugTrace;
	import com.utils.GetMovieClip;
	import com.utils.SWFLoader;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	
	import picPoll.Poll;
	
	/**
	 * ...
	 * @author zhengzihua
	 */
	[SWF(width="1270",height="800",backgroundColor="#FFFFFF")]
	public class Main extends Sprite 
	{
		private var sourceUrl:String="main_lib.swf";
		private var menuData:Array;
		private var xmlUrl:String;
		private var menuXML:XML;
		private var xmlLoader:URLLoader;
		private var menuObj:Object//用来保存先阶段弹出菜单的情况
		private var poll:Poll;
		private var verticalLine:VerticalLine;
		private var arrow:ArrowBtn;
		private var logMc:MovieClip;//log图片
		
		private var scopeNum:int;//可移动的图片个数，只针对第三级菜单
		private var currentMoveNum:int=0;//三级菜单当前的移动状态，0表示没有移动，负表示向上移动，正表示向下移动
		
		private var temp_menu_lv1:Menu;
		private var temp_menu_lv2:Menu;
		private var temp_menu_lv3:Menu;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			loadSource(sourceUrl);
		}
		private function loadSource(url:String):void{
			var loader:SWFLoader=new SWFLoader();
			loader.Load(url,"same");
			loader.addEventListener(Event.COMPLETE,onSourceComplete);
		}
		public function onSourceComplete(e:Event):void{
			//初始化,获取xml数据
			initData();
			xmlLoad();
			
			logMc=GetMovieClip.getMovieClip("LogMc");
			addChild(logMc);
		}
		private function initData():void {
			var cy:int=StaticInfo.MENUEXCURSIONY;
			var cx:int=StaticInfo.VERTICALLINE_X;
			menuData = [];
			xmlUrl = "menu.xml";
			menuObj = { };
			poll = new Poll();
			verticalLine = new VerticalLine();
			addChild(poll);
			addChild(verticalLine);
			poll.x = cx+60;
			poll.y = StaticInfo.POLL_Y;
			verticalLine.x = cx;
			verticalLine.y = cy+10;
		}
		private function xmlLoad():void {
			xmlLoader = new URLLoader(new URLRequest(xmlUrl));
			xmlLoader.addEventListener(Event.COMPLETE, onComplete);
		}
		
		private function onComplete(e:Event):void 
		{
			menuXML = new XML(xmlLoader.data);
			showMenu(null);
			
		}
		/**
		 *	按照xml信息显示菜单 
		 * @param currentMenu 当前menu
		 * 
		 */		
		private function showMenu(currentMenu:Menu):void {//显示菜单
			var level:int;//几级菜单
			var xmlList:XMLList;//菜单列表
			var cy:int;//y轴坐标
			if (currentMenu == null) {
				level = 1;
				xmlList = menuXML.lv1;
				cy = logMc.height;
			}else {
				level = currentMenu.level + 1;
				xmlList = currentMenu.xmlPath[currentMenu.count]["lv" + level];
				cy = currentMenu.p.y;
				//菜单有链接
				var imgList:XMLList=currentMenu.xmlPath[currentMenu.count].item;
				var imgNum:int=imgList.length();
				var imgArr:Array=[];
				var titleArr:Array=[];
				var contentArr:Array=[];
				for(var g:int=0;g<imgNum;g++){
					imgArr.push(imgList[g].@url);
					titleArr.push(imgList[g].@title);
					contentArr.push(imgList[g]);
				}
				poll.pollStop();
				if(imgArr.length>0){
					poll.setArr(imgArr,titleArr,contentArr);
					poll.addEventListener(StaticInfo.JPGLOADERED, function(e:EventZheng):void{addLastBall(e,currentMenu,level)});
					addChild(poll);
					DebugTrace.dtrace("code info Main.as:["+imgArr+"]");
				}else {
					DebugTrace.dtrace("code info Main.as:caption=菜单没有连接。");
				}
			}
			
			if (xmlList.length() == 0) {
				return;
			}	
			var num:int = 0;
			var chagedLevel:Boolean = true;
			menuObj["lv" + level] = [];
			
			for each(var i:XML in xmlList) {
				var txt:String = i.@caption;
				var menu:Menu = new Menu(txt,level,num,currentMenu,xmlList);
				addChild(menu);
				
				if(level==1){
					menu.x = StaticInfo.MENUEXCURSIONX_1;
					menu.y = StaticInfo.ROWSPACE_1 * num+cy;
				}else if(level==2){
					menu.x = StaticInfo.MENUEXCURSIONX_2;
					menu.y = StaticInfo.ROWSPACE_2 * num+cy;
				}else{
					menu.x = StaticInfo.MENUEXCURSIONX_3;
					menu.y = StaticInfo.ROWSPACE_3 * num + StaticInfo.MENUEXCURSIONY;
					menu.isShow();
				}
				menu.p = new Point(menu.x, menu.y);
				menu.addEventListener(StaticInfo.MENUCLICK, onMenuClick);
				menuObj["lv" + level].push(menu);
				if (chagedLevel) {
					chagedLevel = false;
					menuObj.currentLevel = level;//如果放在外面,不管有没有下级菜单,level值都会被重写.
				}
				num++;
				DebugTrace.dtrace("code info Main.as:caption=" + txt);	
			}	
			
			delArrow();
			if (level == 3) {//摆放arrow
				var listNum:int=xmlList.length();
				scopeNum=listNum-23;
				if(scopeNum<=0){
					scopeNum==0;
				}else{
					addArrow(StaticInfo.MENUEXCURSIONX_3 + 150, StaticInfo.MENUEXCURSIONY - 10);
				}
			}
		}
		private function onMenuClick(e:Event):void 
		{		
			//显示子菜单
			//播放小球动画，线条动画
			var menu:Menu = e.currentTarget as Menu;
			var clickLevel:int = menu.level;//被点击的菜单的层级
			var currentLevel:int = menuObj.currentLevel;//当前菜单打开的层数
			var ball:BallAndLine = getChildByName("lv" + clickLevel + "_mc") as BallAndLine;
			var sp:Point;
			var ep:Point;
			
			if(this["temp_menu_lv"+clickLevel]!=null){
				(this["temp_menu_lv"+clickLevel] as Menu).menu.resume();
			}
			this["temp_menu_lv"+clickLevel]=menu;
			menu.menu.onClick();
			
			
			if (currentLevel > clickLevel) {//这种情况下,小球肯定存在
				delMenu(menu);
				showMenu(menu);
				ball.stopMoveBall();
				
				sp = new Point(ball.ball.x, ball.ball.y);
				var level:int = clickLevel;
				while (currentLevel > level ) {
					level++;
					var delBall:BallAndLine=getChildByName("lv" + level + "_mc") as BallAndLine;
					if (delBall != null) {
						delBall.delThis();
					}
				}
				if (clickLevel == 1) {
					ep = new Point(ball.ball.x, menu.y);
				}else {
					ep = new Point(ball.ball.x, menu.y - menu.parentMenu.y);
				}
				ball.moveBallAndLine(sp, ep);
			}else if(currentLevel==clickLevel){
				showMenu(menu);
				if (ball == null) {//没有代表该层级的小球
					ball = new BallAndLine();
					addChild(ball);
					ball.name = "lv" + clickLevel + "_mc";
					if (clickLevel == 1) {
						ball.x = -StaticInfo.EXCURSION;
						ball.y = +StaticInfo.EXCURSION;
						ball.isLevel1 = true;
						ball.createBallAndLine(new Point(menu.x, menu.y));
					}else {	
						ball.x = menu.parentMenu.x-StaticInfo.EXCURSION;
						ball.y = menu.parentMenu.y+StaticInfo.EXCURSION;
						ball.createBallAndLine(new Point(menu.x-menu.parentMenu.x, menu.y-menu.parentMenu.y));
					}
				}else {//小球已经存在,且currentLevel==clickLevel
					ball.stopMoveBall();
					sp = new Point(ball.ball.x, ball.ball.y);
					if (clickLevel == 1) {
						ep = new Point(ball.ball.x, menu.y);
					}else{
						ep = new Point(ball.ball.x, menu.y-menu.parentMenu.y);
					}
					ball.moveBallAndLine(sp, ep);
				}	
			}
			//移动verticalLine上的小球
			if(clickLevel==3){
				var verticalBall:Ball=verticalLine.ball;
				var vsp:Point=new Point(verticalBall.x,verticalBall.y);
				//var vep:Point=new Point(verticalBall.x,menu.count*StaticInfo.ROWSPACE_3+StaticInfo.EXCURSION);
				var vep:Point=new Point(verticalBall.x,menu.y-verticalLine.y+10);
				verticalBall.moveBall(vsp,vep);
			}
		}
				
		private function delMenu(currentMenu:Menu):void {
			var clickLevel:int = currentMenu.level;//被点击的菜单的层级
			var maxLevel:int = menuObj.currentLevel;//当前菜单打开的层数
			while (maxLevel > clickLevel) {
				if(menuObj["lv" + maxLevel]!=undefined){
					var num:int = menuObj["lv" + maxLevel].length;
					for (var i:int = 0; i < num; i++ ) {
						menuObj["lv" + maxLevel][i].delMenu();
					}
				}
				maxLevel--;
				menuObj.currentLevel = maxLevel;
			}
		}
		private function addLastBall(e:EventZheng, currentMenu:Menu, level:int):void {
			poll.removeEventListener(StaticInfo.JPGLOADERED, function(e:EventZheng):void{addLastBall(e,currentMenu,level)});
			menuObj.currentLevel = level;
			//这里有一个弊端，最后显示连接的，一定是lv4，如果是别的层，就不好用了。
			var ball:BallAndLine = getChildByName("lv4_mc") as BallAndLine;
			if (ball == null) {
				
			}else {
				removeChild(ball);
				ball = null;
			}
			ball = new BallAndLine();
			ball.name = "lv4_mc";
			addChild(ball);	
			ball.x = currentMenu.x-StaticInfo.EXCURSION;
			ball.y = currentMenu.y+StaticInfo.EXCURSION;
			setTimeout(function ():void{ball.createBallAndLine(new Point(poll.x-currentMenu.x+e.obj.p.x,poll.y-currentMenu.y+e.obj.p.y))},500);
		}
		private function addArrow(cx:int,cy:int):void {
			arrow = new ArrowBtn();
			arrow.x = cx;
			arrow.y = cy;
			addChild(arrow);
			arrow.addEventListener(StaticInfo.UPARROW_CLICK, onup);
			arrow.addEventListener(StaticInfo.DOWNARROW_CLICK, onDown);
			
		}
		private function delArrow():void {
			if (arrow!=null&&arrow.parent != null) {
				arrow.removeEventListener(StaticInfo.UPARROW_CLICK, onup);
				arrow.removeEventListener(StaticInfo.DOWNARROW_CLICK, onDown);
				arrow.delArrow();
			}
		}
		private function onDown(e:Event):void 
		{
			DebugTrace.dtrace("code info Main.as:on arrow down");
			if(currentMoveNum>0&&scopeNum!=0){
				currentMoveNum--;
				lv3Move(StaticInfo.ROWSPACE_3);
			}else{
				DebugTrace.dtrace("code info Main.as:不能向下移动了");
			}
		}
		
		private function onup(e:Event):void 
		{
			DebugTrace.dtrace("code info Main.as:on arrow up");
			if(currentMoveNum<scopeNum&&scopeNum!=0){
				currentMoveNum++;
				lv3Move(-StaticInfo.ROWSPACE_3);
			}else{
				DebugTrace.dtrace("code info Main.as:不能向上移动了");
			}
		}
		private function lv3Move(step:int):void {
			var num:int = menuObj.lv3.length;
			for (var i:int; i < num; i++ ) {
				var menu:Menu = menuObj.lv3[i] as Menu;
				var cy:int=step+menu.y;
				var targetY:int=StaticInfo.ROWSPACE_3 * (i-currentMoveNum) + StaticInfo.MENUEXCURSIONY;
				TweenMax.to(menu, 0.5, { bezier:[ { y:targetY } ], orientToBezier:true, ease:Cubic.easeOut } );
			}
		}
	}
	
}